/**
 * @description 
 * 程序说明：基于TCP的聊天例程--客户端程序部分
 * @pageNo 452
 * @author mlmoria
 * @date 2017年5月2日
 * 
 */
package No12Net.Chart.Demo;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class J_ChatClient extends JFrame
{

	private ObjectInputStream m_input;//输入流
	private ObjectOutputStream m_output;//输出流 
	private JTextField m_enter;//输入区域
	private JTextArea m_display;//显示区域
	
	public J_ChatClient(){
		super("聊天程序客户端");
		Container c=getContentPane();
		m_enter=new JTextField();
		m_enter.setEnabled(false);
		m_enter.addActionListener(new ActionListener(){			

			@Override
			public void actionPerformed(ActionEvent event) {
			   String s=event.getActionCommand();
			   try {
				m_output.writeObject(s);
				m_output.flush();
				mb_displayAppend("客户端："+s);
				m_enter.setText("");//清空输入区域原有内容
			} catch (IOException e) {
				System.err.println("发生异常："+e);
				e.printStackTrace();
			}
			
				
			}
			
		});
		c.add(m_enter,BorderLayout.NORTH);
		m_display=new JTextArea();
		c.add(new JScrollPane(m_display),BorderLayout.CENTER);   
		
	}//J_ChartServer构造方法结束
	

	public boolean mb_isEndSesion(String m){
		if(m.equalsIgnoreCase("q"))return(true);
		if(m.equalsIgnoreCase("quit"))return(true);
		if(m.equalsIgnoreCase("exit"))return(true);
		if(m.equalsIgnoreCase("end"))return(true);
		if(m.equalsIgnoreCase("结束"))return(true);
		return(false);
	}
	
	public void mb_displayAppend(String s){
		m_display.append(s+"\n");
		m_display.setCaretPosition(m_display.getText().length());//将光标位置放置到str末尾
		
		m_enter.requestFocusInWindow();//转移输入焦点到输入区域
		
	}//方法mb_displayAppend结束
	
    public void mb_run(String host,int port){
    	
    	try {
    		mb_displayAppend("尝试连接");
    	
			Socket s =new Socket(host,port);
			String m;
			m_output=new ObjectOutputStream(s.getOutputStream());
			m_input=new ObjectInputStream(s.getInputStream());
			m_enter.setEnabled(true);
			do{
				m=(String) m_input.readObject();
				mb_displayAppend("服务端:"+m);
			}while(!mb_isEndSesion(m));//do while 结束
			
			m_output.writeObject("q");
			m_output.flush();			
			m_output.close();
			m_input.close();
			s.close();
			System.exit(0);
			
    	}	
		catch (Exception e) {
			System.err.println("发生异常："+e);
			e.printStackTrace();
			mb_displayAppend("发生异常");
		}
    }
    
    public static void main(String[] args) {
		J_ChatClient app=new J_ChatClient();
		app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		app.setSize(350,150);
		app.setVisible(true);
		if(args.length==0){
			app.mb_run("localhost", 5000);
		}else{
			app.mb_run(args[0], 5000);
		}
	}
	
}
